<?php 
//左侧导航
class SidebarWidget extends Widget 
{
	public function render($data)
	{
		$navigation = M('Action');
		$where = " action_type=1 AND action_id > 0 "; //只查找菜单类型的
		if(!session('?admin')){
			$str = M('role')->where('role_id = %d',session('role_id'))->field('action_list')->find();
			$str = $str['action_list'];
			$str = str_replace(" ","",$str);
			$str = trim($str,',');
			$where .=" AND action_id in(".$str.") ";
		}
		//dump($where);
		//$arr = $navigation->where('action_id in(%s)',$str)->field('action_code')->order('order_id')->select();
		$list = $navigation->where($where)->order('order_id')->select();
		
		
		//dump($list);
		//dump($navigation->getlastsql());

		foreach($list AS $value) {
			$navigationList[$value['action_id']] = $value;
		}

	
/* 		// 取得列的列表
		foreach ($navigationList as $key => $row) {
			$listorder[$key]  = $row['listorder'];
		}
		 
		array_multisort($listorder, SORT_ASC, $navigationList);		 */
		//生成一级目录
		//dump($navigationList);MENU_HOME
		foreach($navigationList as $v){
			$v['action_name'] = L($v['action_name']);
			if(0 == $v['pid']){
				//dump($v);
				
				$menu1[$v['action_id']] =$v;
				//dump($v);
			}else{
				$menu2[$v['action_id']] =$v;
				//$menu1[$v['parent_id']]['have_sun'] = 1;
				//$menu1[$v['parent_id']]['sun'][] = $v;
			}
		}
		

		foreach ($menu2 as $key => $v) {
			//dump($menu2);
			$menu1[$v['pid']]['sun'][]  = $v;
			$menu1[$v['pid']]['have_sun'] = 1;
			//dump($v['parent_id']);
		}
		

		return $this->renderFile ("index",array('menu1'=>$menu1,'menu2'=>$menu2));
	}


	public function render_old($data)
	{
		$user = M('User');
		$navigation = M('Navigation');
		$permission = M('Permission');
		//$value = $user->where("user_id = %d", session('user_id'))->getField('navigation');
		//$menu = unserialize($value);
		

		$list = $navigation->order('listorder')->select();
		$controls = $permission->where('position_id = %d', session('position_id'))->getField('url', true);
		foreach($list AS $value) {
			if(empty($value['module']) or in_array(strtolower($value['module']).'/index', $controls) or session('?admin')){
				$navigationList[$value['id']] = $value;
			}
		}
	
/* 		// 取得列的列表
		foreach ($navigationList as $key => $row) {
			$listorder[$key]  = $row['listorder'];
		}
		 
		array_multisort($listorder, SORT_ASC, $navigationList);		 */
		//生成一级目录
		//dump($navigationList);
		foreach($navigationList as $v){
			if(0 == $v['parent_id']){
				$menu1[$v['id']] =$v;
				//dump($v);
			}else{
				$menu2[$v['id']] =$v;
				//$menu1[$v['parent_id']]['have_sun'] = 1;
				//$menu1[$v['parent_id']]['sun'][] = $v;
			}
		}
		

		foreach ($menu2 as $key => $v) {
			//dump($menu2);
			$menu1[$v['parent_id']]['sun'][]  = $v;
			$menu1[$v['parent_id']]['have_sun'] = 1;
			//dump($v['parent_id']);
		}
		

		return $this->renderFile ("index",array('menu1'=>$menu1,'menu2'=>$menu2));
	}


}
